//
//  HistoryDetaileViewController.m
//  CityFire
//
//  Created by  androidlongs on 2017/11/2.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "HistoryDetaileViewController.h"
#import "RadioButton.h"

#import "UserInfoModel.h"
#import "NSUserDefaults+Tools.h"

@interface HistoryDetaileViewController ()
@property(nonatomic,strong) UIScrollView *mainScrollView ;

@property(nonatomic,assign) BOOL isSelect ;
@property(nonatomic,strong) UITextView *inputResultUITextView;
@property(nonatomic,strong) RadioButton *selectButton;
@end

@implementation HistoryDetaileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"信息详情";
    self.isSelect=NO;
    [self initView];
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.mainScrollView.frame = CGRectMake(0, 0, mainW, mainH);
    self.mainScrollView.contentSize = CGSizeMake(mainW, 1300);
    
}
- (void)initView {
    
    UIScrollView *mainScrollView =[[UIScrollView alloc]init];
    mainScrollView.frame=CGRectMake(0, 0, mainW, mainH);
    [mainScrollView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    [self.view addSubview:mainScrollView];
    self.mainScrollView=mainScrollView;
    UIView *contentView1=[[UIView alloc]init];
    contentView1.frame=CGRectMake(0, 0, mainW, 200);
    [contentView1 setBackgroundColor:[UIColor whiteColor]];
    
    
    UIView *contentView2=[[UIView alloc]init];
    contentView2.frame=CGRectMake(0, CGRectGetMaxY(contentView1.bounds)+15, mainW, 352);
    [contentView2 setBackgroundColor:[UIColor whiteColor]];
    
    
    UIView *contentView3=[[UIView alloc]init];
    contentView3.frame=CGRectMake(0, 582, mainW, 70);
    [contentView3 setBackgroundColor:[UIColor whiteColor]];
    
    
    UIView *contentView4=[[UIView alloc]init];
    contentView4.frame=CGRectMake(0, 667, mainW, 41);
    [contentView4 setBackgroundColor:[UIColor whiteColor]];
    
    UIView *contentView5=[[UIView alloc]init];
    contentView5.frame=CGRectMake(0, 723, mainW, 380);
    [contentView5 setBackgroundColor:[UIColor whiteColor]];
    
    UIView *contentView6=[[UIView alloc]init];
    contentView6.frame=CGRectMake(0, mainH-200, mainW, 200);
    [contentView6 setBackgroundColor:[UIColor colorWithHexString:@"#bbbbbb"]];
    
    
    
    [mainScrollView addSubview:contentView1];
    [mainScrollView addSubview:contentView2];
    [mainScrollView addSubview:contentView3];
    [mainScrollView addSubview:contentView4];
    [mainScrollView addSubview:contentView5];
    [self.view addSubview:contentView6];
    
    
    UILabel *titleLabel =[[UILabel alloc]init];
    titleLabel.frame = CGRectMake(10, 10, mainW-20, 21);
    [titleLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    titleLabel.text=[NSString stringWithFormat:@"设备名称：%@",self.monitorModel.deviceName] ;
    [contentView1 addSubview:titleLabel];
    
    
    
    UILabel *countLabel =[[UILabel alloc]init];
    countLabel.frame=CGRectMake(10, 41, mainW-20, 21);
    [countLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    countLabel.text=[NSString stringWithFormat:@"设备编号：%ld",self.monitorModel.deviceCode] ;
    [contentView1 addSubview:countLabel];
    
    
    UILabel *timeLabel =[[UILabel alloc]init];
    timeLabel.frame=CGRectMake(10, 72, mainW-20, 21);
    [timeLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    
    NSString *historyTime = [[NSDate dateWithTimeIntervalSince1970:self.monitorModel.timeHistory / 1000] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    timeLabel.text=[NSString stringWithFormat:@"时间：%@",historyTime] ;
    [contentView1 addSubview:timeLabel];
    
    UILabel *sourceLabel =[[UILabel alloc]init];
    sourceLabel.frame=CGRectMake(10, 103, mainW-20, 21);
    [sourceLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    sourceLabel.text=[NSString stringWithFormat:@"来源：%@",self.monitorModel.record1] ;
    [contentView1 addSubview:sourceLabel];
    
    UILabel *typeLabel =[[UILabel alloc]init];
    typeLabel.frame=CGRectMake(10, 134, mainW-20, 21);
    [typeLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    typeLabel.text=[NSString stringWithFormat:@"类型：%@",self.monitorModel.record2] ;
    [contentView1 addSubview:typeLabel];
    
    UILabel *contentLabel =[[UILabel alloc]init];
    contentLabel.frame=CGRectMake(10, 164, mainW-20, 21);
    [contentLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    contentLabel.text=[NSString stringWithFormat:@"内容：%@",self.monitorModel.record3] ;
    [contentView1 addSubview:contentLabel];
    
    
    UILabel *diviceInfro =[[UILabel alloc]init];
    diviceInfro.frame=CGRectMake(10, 10, mainW-20, 21);
    [diviceInfro setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceInfro.text=[NSString stringWithFormat:@"设备信息：%@",self.monitorModel.deviceName] ;
    [contentView2 addSubview:diviceInfro];
    
    UILabel *diviceType=[[UILabel alloc]init];
    diviceType.frame=CGRectMake(10, 41, mainW-20, 21);
    [diviceType setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceType.text=[NSString stringWithFormat:@"设备类型：%@",self.monitorModel.deviceType] ;
    [contentView2 addSubview:diviceType];
    
    UILabel *diviceBusinessLabel=[[UILabel alloc]init];
    diviceBusinessLabel.frame=CGRectMake(10, 72, mainW-20, 21);
    [diviceBusinessLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceBusinessLabel.text=[NSString stringWithFormat:@"控制器厂商：%@",self.monitorModel.controllerManufacturer] ;
    [contentView2 addSubview:diviceBusinessLabel];
    
    UILabel *diviceCountLabel=[[UILabel alloc]init];
    diviceCountLabel.frame=CGRectMake(10, 103, mainW-20, 21);
    [diviceCountLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceCountLabel.text=[NSString stringWithFormat:@"控制器型号：%@",self.monitorModel.controllerSpec] ;
    [contentView2 addSubview:diviceCountLabel];
    
    
    UILabel *diviceCountLabel2=[[UILabel alloc]init];
    diviceCountLabel2.frame=CGRectMake(10, 134, mainW-20, 21);
    [diviceCountLabel2 setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceCountLabel2.text=[NSString stringWithFormat:@"产品编号：%@",self.monitorModel.itemCode] ;
    [contentView2 addSubview:diviceCountLabel2];
    
    UILabel *diviceNumLabel=[[UILabel alloc]init];
    diviceNumLabel.frame=CGRectMake(10, 165, mainW-20, 21);
    [diviceNumLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceNumLabel.text=[NSString stringWithFormat:@"探测器数量：%ld",self.monitorModel.detectorAmount] ;
    [contentView2 addSubview:diviceNumLabel];
    
    
    UILabel *diviceInTimeLabel=[[UILabel alloc]init];
    diviceInTimeLabel.frame=CGRectMake(10, 196, mainW-20, 21);
    [diviceInTimeLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceInTimeLabel.text=[NSString stringWithFormat:@"采购时间：%@",self.monitorModel.purchaseTime] ;
    [contentView2 addSubview:diviceInTimeLabel];
    
    UILabel *diviceAdressLabel=[[UILabel alloc]init];
    diviceAdressLabel.frame=CGRectMake(10, 227, mainW-20, 21);
    [diviceAdressLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    diviceAdressLabel.text=[NSString stringWithFormat:@"设置备所在地：%@",self.monitorModel.deviceAddress] ;
    [contentView2 addSubview:diviceAdressLabel];
    
    
    UILabel *divicesaveLabel=[[UILabel alloc]init];
    divicesaveLabel.frame=CGRectMake(10, 258, mainW-20, 21);
    [divicesaveLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    divicesaveLabel.text=[NSString stringWithFormat:@"维保年限：%@",self.monitorModel.wbnx] ;
    [contentView2 addSubview:divicesaveLabel];
    
    UILabel *divicesaveCantLabel=[[UILabel alloc]init];
    divicesaveCantLabel.frame=CGRectMake(10, 289, mainW-20, 21);
    [divicesaveCantLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    divicesaveCantLabel.text=[NSString stringWithFormat:@"维护联系人：%@",self.monitorModel.linkMan] ;
    [contentView2 addSubview:divicesaveCantLabel];
    
    UILabel *divicesaveCantMobLabel=[[UILabel alloc]init];
    divicesaveCantMobLabel.frame=CGRectMake(10, 320, mainW-20, 21);
    [divicesaveCantMobLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    divicesaveCantMobLabel.text=[NSString stringWithFormat:@"联系人电话：%@",self.monitorModel.phone] ;
    [contentView2 addSubview:divicesaveCantMobLabel];
    
    
    
    UILabel *divicesavelongitudeLabel=[[UILabel alloc]init];
    divicesavelongitudeLabel.frame=CGRectMake(10, 10, mainW-20, 21);
    [divicesavelongitudeLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    divicesavelongitudeLabel.text=[NSString stringWithFormat:@"经度：%@",self.monitorModel.longitude] ;
    [contentView3 addSubview:divicesavelongitudeLabel];
    
    
    UILabel *divicelatitudeLabel=[[UILabel alloc]init];
    divicelatitudeLabel.frame=CGRectMake(10, 41, mainW-20, 21);
    [divicelatitudeLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    divicelatitudeLabel.text=[NSString stringWithFormat:@"纬度：%@",self.monitorModel.latitude] ;
    [contentView3 addSubview:divicelatitudeLabel];
    
    
    UILabel *phoneLabel=[[UILabel alloc]init];
    phoneLabel.frame=CGRectMake(10, 10, mainW-20, 21);
    [phoneLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    phoneLabel.text=[NSString stringWithFormat:@"联系电话：%@",@"13000000000"] ;
    [contentView4 addSubview:phoneLabel];
    
    UILabel *inputResultLabel=[[UILabel alloc]init];
    inputResultLabel.frame=CGRectMake(10, 10, mainW-20, 21);
    [inputResultLabel setTextColor:[UIColor colorWithHexString:@"#656565"]];
    inputResultLabel.text=[NSString stringWithFormat:@"填写处理结果"] ;
    [contentView5 addSubview:inputResultLabel];
    
    UITextView *inputResultUITextView=[[UITextView alloc]init];
    [inputResultUITextView setBackgroundColor:[UIColor colorWithHexString:@"#ababab"]];
    inputResultUITextView.frame=CGRectMake(10, 41, mainW-20, 322);
    [inputResultUITextView setTextColor:[UIColor colorWithHexString:@"#656565"]];
    [contentView5 addSubview:inputResultUITextView];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:inputResultUITextView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = inputResultUITextView.bounds;
    maskLayer.path = maskPath.CGPath;
    inputResultUITextView.layer.mask  = maskLayer;
    inputResultUITextView.text = self.monitorModel.recordUsrLog;
    NSLog(@"处理结果 %@ ",self.monitorModel.recordUsrLog);
    NSString *string =self.monitorModel.recordUsrLog;
    NSArray *array = [string componentsSeparatedByString:@":"]; //从字符A中分隔成2个元素的数组
    if (array&&array.count==2) {
        string =array[1];
    }
    self.inputResultUITextView=inputResultUITextView;
    self.inputResultUITextView .text= string;
    
    
    RadioButton *btn=[[RadioButton alloc]init];
    btn.frame=CGRectMake(10, 20, mainW-20, 21);
    [btn addTarget:self action:@selector(onRadioButtonValueChanged:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"是否标记为误报" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    
    [contentView6 addSubview:btn];
    self.selectButton = btn;
    
    UIButton *commitButton=[[UIButton alloc]init];
    [commitButton setBackgroundColor:[UIColor blueColor]];
    [commitButton setTitle:@"更新信息" forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(updateBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    commitButton.frame=CGRectMake(20, 71, mainW-42, 30);
    [commitButton setTintColor:[UIColor whiteColor]];
    [contentView6 addSubview:commitButton];
    UIBezierPath *maskPathButton = [UIBezierPath bezierPathWithRoundedRect:commitButton.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayerButton = [[CAShapeLayer alloc] init];
    maskLayerButton.frame = commitButton.bounds;
    maskLayerButton.path = maskPathButton.CGPath;
    commitButton.layer.mask  = maskLayerButton;
    
    
    
    
}
- (void) onRadioButtonValueChanged:(RadioButton *) button{
    [button setSelected:!self.isSelect];
    self.isSelect=!self.isSelect;
}
- (void)updateBtnClicked:(UIButton*)sender
{
    NSString *result =self.inputResultUITextView.text;
    if (!self.inputResultUITextView.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入处理结果"];
        return;
    }
    
    [self commitInfromation:@{@"record_usr_log" : self.inputResultUITextView.text, @"misinformation" : self.selectButton.selected ? @(0) : @(1)}];
}

- (void)commitInfromation:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    NSInteger select =1;
    if([self.selectButton isSelected]){
        select=1;
    }else{
        select =0;
    }
    if (model) {
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        //表名
        [dic setObject:self.monitorModel.tableName forKey:@"table_name"];
        [dic setObject:model.userName forKey:@"username"];
        //主键
        [dic setObject:[NSString stringWithFormat:@"%ld",self.monitorModel.num] forKey:@"num"];
        //处理意见
        [dic setObject:self.inputResultUITextView.text forKey:@"record_usr_log"];
        //是否误报
        [dic setObject:[NSString stringWithFormat:@"%ld",select] forKey:@"misinformation"];
        [dic setObject:model.seeeionID forKey:@"sessionid"];
        [YQNetworking requestWithUrl:kWARNING_DEAL_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:dic progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //        /api-w/warning/queryRealTimeWarning?
            //        username=admin&x_day=7&pageNo=1&pageSize=5
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
                
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [SVProgressHUD showErrorWithStatus:@"服务异常"];
            }
            
        } failBlock:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
